package com.zhdl.network.strategy.modbustcp;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import com.zhdl.network.other_protocol.modbustcp.after.ModbusTcpAfterService;
import com.zhdl.network.other_protocol.modbustcp.analysis.ModBusTcpAnalysisImpl;
import com.zhdl.network.strategy.common.NetworkStrategy;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

/**
 * Hardware-ModbusTcp指令发送
 */
public class ModbusTcpStrategy implements NetworkStrategy {

    public final BehaviorPerform behaviorPerform;
    public String TcpPort;

    public ModbusTcpStrategy(String TcpPort, BehaviorPerform behaviorPerform) {
        this.TcpPort = TcpPort;
        this.behaviorPerform = behaviorPerform;

    }

    @Override
    public void sendCmd(String cmd) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());
        instruction.setInstruction(cmd);
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(ModbusTcpAfterService.MODBUS_TCP_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
        instruction.setPortKey(TcpPort);
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(ModBusTcpAnalysisImpl.MODBUS_TCP_ANALYSIS);
        behaviorPerform.perform(instruction);
    }
}
